import $ from 'jquery'
import * as echarts from 'echarts'
let api = 'http://forest.kmdsoft.net/admin/Region/getRegionGeoJson?grade=city'
import { Message } from 'element-ui'
export const getEcharts = () => {
  // 基于准备好的dom，初始化echarts实例
  var myChart = echarts.init(document.getElementById('main'))
  myChart.showLoading()
  myChart.off('click')
  myChart.on('click', function(params) {
    // const api = ''
    this.data = {
      x: params.name,
      y: params.value
    }
    console.log(this.data)

    if (this.data.x === '忻州市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=140900_full'
      getEcharts(echarts)
      console.log(11)
    } else if (this.data.x === '大同市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=140200_full'
      getEcharts(echarts)
    } else if (this.data.x === '朔州市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=140600_full'
      getEcharts(echarts)
    } else if (this.data.x === '晋城市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=140500_full'
      getEcharts(echarts)
    } else if (this.data.x === '长治市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=140400_full'
      getEcharts(echarts)
    } else if (this.data.x === '阳泉市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=140300_full'
      getEcharts(echarts)
    } else if (this.data.x === '太原市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=140100_full'
      getEcharts(echarts)
    } else if (this.data.x === '运城市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=140800_full'
      getEcharts(echarts)
    } else if (this.data.x === '晋中市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=140700_full'
      getEcharts(echarts)
    } else if (this.data.x === '临汾市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=141000_full'
      getEcharts(echarts)
    } else if (this.data.x === '吕梁市') {
      api = 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=141100_full'
      getEcharts(echarts)
    }
    if (this.data.x !== '忻州市' && this.data.x !== '大同市' && this.data.x !== '朔州市' && this.data.x !== '晋城市' && this.data.x !== '长治市' && this.data.x !== '阳泉市' && this.data.x !== '太原市' && this.data.x !== '运城市' && this.data.x !== '晋中市' && this.data.x !== '临汾市' && this.data.x !== '吕梁市') {
      Message.info(`暂无${this.data.x}的数据`)
    }
  })
  $.get(
    api,
    function(geoJson) {
      myChart.hideLoading()
      echarts.registerMap('HK', geoJson)
      myChart.setOption(

        (this.option = {
          title: {
            sublink:
          'http://zh.wikipedia.org/wiki/%E9%A6%99%E6%B8%AF%E8%A1%8C%E6%94%BF%E5%8D%80%E5%8A%83#cite_note-12'
          },
          tooltip: {
            trigger: 'item',
            formatter: '{b}<br/>{c} (p / km2)'
          },
          toolbox: {
            show: true,
            orient: 'vertical',
            left: 'right',
            top: 'center',
            feature: {
              dataView: { readOnly: false },
              restore: {},
              saveAsImage: {}
            }
          },
          visualMap: {
            min: 800,
            max: 50000,
            text: ['High', 'Low'],
            realtime: false,
            calculable: true,
            inRange: {
              color: ['#d4ea78', '#e8f45e', '#ec652b']
            }
          },
          series: [
            {
              name: '香港18区人口密度',
              type: 'map',
              map: 'HK',
              label: {
                show: true
              },
              data: [
                { name: '大同市', value: 20057.34 },
                { name: '朔州市', value: 15477.48 },
                { name: '忻州市', value: 31686.1 },
                { name: '晋城市', value: 6992.6 },
                { name: '长治市', value: 24045.49 },
                { name: '阳泉市', value: 30689.64 },
                { name: '太原市', value: 27659.78 },
                { name: '运城市', value: 35180.97 },
                { name: '晋中市', value: 25204.26 },
                { name: '临汾市', value: 21900.9 },
                { name: '吕梁市', value: 4918.26 }
              ],
              // 自定义名称映射
              nameMap: {
                'Central and Western': '中西区',
                Eastern: '东区',
                Islands: '离岛',
                'Kowloon City': '九龙城',
                'Kwai Tsing': '葵青',
                'Kwun Tong': '观塘',
                North: '北区',
                'Sai Kung': '西贡',
                'Sha Tin': '沙田',
                'Sham Shui Po': '深水埗',
                Southern: '南区',
                'Tai Po': '大埔',
                'Tsuen Wan': '荃湾',
                'Tuen Mun': '屯门',
                'Wan Chai': '湾仔',
                'Wong Tai Sin': '黄大仙',
                'Yau Tsim Mong': '油尖旺',
                'Yuen Long': '元朗'
              }
            }
          ]
        })
      )
    })
}
